package org.jeecg.modules.demo.applyapartemployee.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.applyapartemployee.entity.ZhzfApplyApartEmployeeVo;
import org.jeecg.modules.demo.applyapartemployee.entity.ZnzfApplyApartEmployee;
import org.jeecg.modules.demo.applyapartemployee.service.IZnzfApplyApartEmployeeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.applyinfo.entity.ZnzfApply;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfDetermineApart;
import org.jeecg.modules.demo.applyinfo.service.IZnzfDetermineApartService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 单位报名人员
 * @Author: jeecg-boot
 * @Date:   2022-03-19
 * @Version: V1.0
 */
@Api(tags="单位报名人员")
@RestController
@RequestMapping("/applyapartemployee/znzfApplyApartEmployee")
@Slf4j
public class ZnzfApplyApartEmployeeController extends JeecgController<ZnzfApplyApartEmployee, IZnzfApplyApartEmployeeService> {
	@Autowired
	private IZnzfApplyApartEmployeeService znzfApplyApartEmployeeService;

	 @Autowired
	 private IZnzfDetermineApartService znzfDetermineApartService;

	 @Autowired
	 private ISysUserService sysUserService;

	 @ApiOperation(value = "单位报名人员-入围操作",notes = "让某人入围")
	 @PostMapping(value = "/shortlist")
	 public Result shortlistSb(@RequestBody ZnzfApplyApartEmployee table){
	 	int result = znzfApplyApartEmployeeService.shortlistSb(table.getId());
	 	if(result <= 0)
	 		return Result.error("更新失败");
	 	return Result.OK();
	 }

	 @ApiOperation(value = "单位报名人员-添加",notes = "报名")
	 @PostMapping(value = "/submitapply")
	 public Result<String> submitApply(HttpServletRequest request, @RequestBody ZnzfApply znzfApply){
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 String uid = sysUserService.getUserByName(name).getId();
		 int res = znzfApplyApartEmployeeService.submitApply(znzfApply.getId(),uid);
		 if(res <= 0)
		 	Result.error("插入失败");
		 if(res == 2){
			 Result<String> result = new Result<String>();
			 result.setSuccess(false);
			 result.setMessage("已报名,不可重复报名");
			 return result;
		 }
		 return Result.OK("申请成功");
	 }

	/**
	 * 分页列表查询
	 *
	 * @param znzfApplyApartEmployee
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "单位报名人员-分页列表查询")
	@ApiOperation(value="单位报名人员-分页列表查询", notes="单位报名人员-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZnzfApplyApartEmployee>> queryPageList(ZnzfApplyApartEmployee znzfApplyApartEmployee,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZnzfApplyApartEmployee> queryWrapper = QueryGenerator.initQueryWrapper(znzfApplyApartEmployee, req.getParameterMap());
		Page<ZnzfApplyApartEmployee> page = new Page<ZnzfApplyApartEmployee>(pageNo, pageSize);
		IPage<ZnzfApplyApartEmployee> pageList = znzfApplyApartEmployeeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param znzfApplyApartEmployee
	 * @return
	 */
	@AutoLog(value = "单位报名人员-添加")
	@ApiOperation(value="单位报名人员-添加", notes="单位报名人员-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZnzfApplyApartEmployee znzfApplyApartEmployee) {
		znzfApplyApartEmployeeService.save(znzfApplyApartEmployee);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param znzfApplyApartEmployee
	 * @return
	 */
	@AutoLog(value = "单位报名人员-编辑")
	@ApiOperation(value="单位报名人员-编辑", notes="单位报名人员-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZnzfApplyApartEmployee znzfApplyApartEmployee) {
		znzfApplyApartEmployeeService.updateById(znzfApplyApartEmployee);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "单位报名人员-通过id删除")
	@ApiOperation(value="单位报名人员-通过id删除", notes="单位报名人员-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		znzfApplyApartEmployeeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "单位报名人员-批量删除")
	@ApiOperation(value="单位报名人员-批量删除", notes="单位报名人员-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.znzfApplyApartEmployeeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "单位报名人员-通过id查询")
	@ApiOperation(value="单位报名人员-通过id查询", notes="单位报名人员-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZnzfApplyApartEmployee> queryById(@RequestParam(name="id",required=true) String id) {
		ZnzfApplyApartEmployee znzfApplyApartEmployee = znzfApplyApartEmployeeService.getById(id);
		if(znzfApplyApartEmployee==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfApplyApartEmployee);
	}

	 @AutoLog(value = "单位报名人员-通过apply_list_id和apart_id查询")
	 @ApiOperation(value="单位报名人员-通过apply_list_id和apart_id查询", notes="单位报名人员-通过apply_list_id和apart_id查询")
	 @GetMapping(value = "/queryByApplyListIdAndApartId")
	 public Result<Page<ZhzfApplyApartEmployeeVo>> queryByApplyListIdAndApartId(@RequestParam(name="applyId",required=true) String apply_list_id,
																		@RequestParam(name="apartId",required=true) String apart_id,
																		@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																		@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																		@RequestParam(name="name", required = false) String name,
																		HttpServletRequest req) {
		 ZnzfDetermineApart znzfDetermineApart = znzfDetermineApartService.searchByApplyListIdAndApartId(apply_list_id, apart_id);
		 if (znzfDetermineApart == null) {
			 return Result.error("未找到对应数据", null);
		 }
		 ZnzfApplyApartEmployee znzfApplyApartEmployee = new ZnzfApplyApartEmployee();
		 List<ZnzfApplyApartEmployee> znzfApplyApartEmployeeList = znzfApplyApartEmployeeService.searchsByCheckDetermineApartId(znzfDetermineApart.getId());
		 if (znzfApplyApartEmployeeList == null) {
			 return Result.error("未找到对应数据", null);
		 }
		 List<ZhzfApplyApartEmployeeVo> zhzfApplyApartEmployeeVoList = new ArrayList<>();
		 for (ZnzfApplyApartEmployee tmp : znzfApplyApartEmployeeList) {
			 ZhzfApplyApartEmployeeVo zhzfApplyApartEmployeeVo = new ZhzfApplyApartEmployeeVo();
			 zhzfApplyApartEmployeeVo = zhzfApplyApartEmployeeVo.zhzfApplyApartEmployeeToZhzfApplyApartEmployeeVo(tmp);
			 SysUser sysUser = sysUserService.getById(tmp.getUserId());
			 String realname = sysUser.getRealname();
			 if(name != null && !realname.contains(name))
			 	continue;
			 zhzfApplyApartEmployeeVo.setName(sysUser.getRealname());
			 zhzfApplyApartEmployeeVo.setTel(sysUser.getPhone());
			 zhzfApplyApartEmployeeVoList.add(zhzfApplyApartEmployeeVo);
		 }
		 if (zhzfApplyApartEmployeeVoList.isEmpty()) {
			 Page<ZhzfApplyApartEmployeeVo> page = new Page<>(pageNo, pageSize);
			 Result<Page<ZhzfApplyApartEmployeeVo>> result = new Result<Page<ZhzfApplyApartEmployeeVo>>();
			 page.setSize(pageSize);
			 page.setCurrent(pageNo);
			 page.setTotal(0);
			 page.setPages(1);
			 page.setRecords(zhzfApplyApartEmployeeVoList);
			 result.setResult(page);
			 result.setSuccess(true);
			 return result;
		 }
//		 QueryWrapper<ZnzfApplyApartEmployee> queryWrapper = QueryGenerator.initQueryWrapper(znzfApplyApartEmployee, req.getParameterMap());
//		 queryWrapper.lambda().eq(ZnzfApplyApartEmployee::getCheckDetermineApartId,znzfDetermineApart.getId());
//		 Page<ZnzfApplyApartEmployee> page = new Page<ZnzfApplyApartEmployee>(pageNo, pageSize);
//		 IPage<ZnzfApplyApartEmployee> pageList = znzfApplyApartEmployeeService.page(page, queryWrapper);

		 //当前页
//		 int current = 1;
		 //每页数据条数
//		 int size = 10;
		 Page<ZhzfApplyApartEmployeeVo> page = new Page<>(pageNo, pageSize);
		 int count = zhzfApplyApartEmployeeVoList.size();
		 List<ZhzfApplyApartEmployeeVo> pageList = new ArrayList<>();
//计算当前页第一条数据的下标
		 int currId = pageNo > 1 ? (pageSize - 1) * pageSize : 0;
		 for (int i = 0; i < pageSize && i < count - currId; i++) {
			 pageList.add(zhzfApplyApartEmployeeVoList.get(currId + i));
		 }
		 page.setSize(pageSize);
		 page.setCurrent(pageNo);
		 page.setTotal(count);
//计算分页总页数
		 page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
		 page.setRecords(pageList);

		 Result<Page<ZhzfApplyApartEmployeeVo>> result = new Result<Page<ZhzfApplyApartEmployeeVo>>();
		 result.setResult(page);
		 result.setSuccess(true);
		 return result;
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param znzfApplyApartEmployee
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZnzfApplyApartEmployee znzfApplyApartEmployee) {
        return super.exportXls(request, znzfApplyApartEmployee, ZnzfApplyApartEmployee.class, "单位报名人员");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZnzfApplyApartEmployee.class);
    }

}
